package com.masterclient.app.businessmodule.homemodule.contract;

import com.eoner.baselib.container.CommonBaseResponse;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselib.utils.sp.SPUtils;
import com.eoner.baselibrary.utils.CookieNewUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.common.config.Config;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.masterclient.app.modules.income.model.HomeDataBean;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.MultipartBody;

/**
 * Created by TMVPHelper on 2017/07/18
 */
public class HomePresenter extends HomeContract.Presenter {

    @Override
    public void getIndexData(int page) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        builder.addFormDataPart("app_version", CookieNewUtil.getVersionName(Utils.getContext()));
        builder.addFormDataPart("platform","android");
        builder.addFormDataPart("token", SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));

        Observable<CommonBaseResponse<HomeDataBean>> observable = RetrofitUtil2.getInstance().getIndexData(builder.build().parts(),
                NetEnvManager.BASE_SURL+"1/index/"+page+"-"+"20"+".html");
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse<HomeDataBean>>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.showHomeList(bean.data);
            } else {
                mView.showHomeListFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.showHomeListFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void addSubscribe(String eventId) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);//表单类型

        builder.addFormDataPart("event_id",eventId);
        builder.addFormDataPart("token", SPUtils.getInstance().getString(Config.CUSTOMER_TOKEN));

        Observable<CommonBaseResponse> observable = RetrofitUtil2.getInstance().addSubscribe(builder.build().parts());
        Disposable disposable = toSubscribe(observable, (Consumer<CommonBaseResponse>) bean -> {
            if (bean != null && 0 == bean.getCode()) {
                mView.subscribeSuccess();
            } else {
                mView.subscribeFailed(bean.getMsg());
            }
        }, new ThrowableConsumer() {
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.subscribeFailed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}